﻿using Catastrophe.BasePC.UI.UI;
using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Catastrophe.BasePC.UI.UIMode
{
    /// <summary>
    /// 角色管理
    /// </summary>
    public partial class UcSysUserModel : ObservableObject
    {
        /// <summary>
        /// 
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<SysUserEntity> dataList;
        /// <summary>
        /// 选中的
        /// </summary>
        [ObservableProperty]
        private SysUserEntity selectDataList;
        /// <summary>
        /// 删除
        /// </summary>
        public ICommand DelCommand { get; set; }
        /// <summary>
        /// 编辑
        /// </summary>
        public ICommand EditCommand { get; set; }
        public ICommand AddCommand { get; set; }
        public ICommand EditRoleCommand { get; set; }
        /// <summary> 
        /// 
        /// </summary>
        private SysUserDal _baseUserDal { get; set; }
        public UcSysUserModel()
        {
            dataList = new ObservableCollection<SysUserEntity>();
            DelCommand = new RelayCommand(DelCommandMethod);
            EditCommand = new RelayCommand(EditCommandMethod);
            AddCommand = new RelayCommand(AddCommandMethod);
            EditRoleCommand = new RelayCommand(EditRoleCommandMethod);
            _baseUserDal = new SysUserDal();
            DtRefresh();
        }

        private void EditRoleCommandMethod()
        {
            if (SelectDataList==null)
            {
                return;
            }
            WdRoleEdit wdRole = new WdRoleEdit(SelectDataList);
            wdRole.ShowDialog() ;
        }

        private UcSysUserEdit CreateUC()
        {
            UcSysUserEdit uc = new UcSysUserEdit();
            uc.WindowClose += Uc_WindowClose;
            return uc;
        }

        private void Uc_WindowClose(bool obj)
        {
            if (obj)
            {
                DtRefresh();
            }
        }

        private void AddCommandMethod()
        {
            UcSysUserEdit ucSysUserEdit = CreateUC();
            ucSysUserEdit.windowType = MyEnum.WindowTypeEnum.Add;
            ucSysUserEdit.ShowDialog();
        }

        private void EditCommandMethod()
        {
            if (SelectDataList != null)
            {
                UcSysUserEdit ucSysUserEdit = CreateUC();
                ucSysUserEdit.windowType = MyEnum.WindowTypeEnum.Edit;
                ucSysUserEdit.mode.selectDataList = SelectDataList;
                ucSysUserEdit.ShowDialog();
            }
        }

        private void DelCommandMethod()
        {
            if (SelectDataList != null)
            {
                if (SelectDataList.UserName=="admin")
                {
                    MessageBox.Show("admin账号不可以删除");
                    return;
                }
                _baseUserDal.DeleteById(SelectDataList.Id);
                DtRefresh();
            }
        }

        private void DtRefresh()
        {
            dataList.Clear();
            var data = _baseUserDal.QueryUserAll();
            data.ForEach(x => dataList.Add(x));
        }
    }
}
